package com.shike.web.service;

import com.google.common.collect.ImmutableMultimap;
import com.shike.core.dto.JwtUserDto;
import com.shike.core.utils.JWTUtil;
import com.shike.core.utils.R;
import com.shike.core.utils.SysCode;
import com.shike.core.utils.redis.RedisUtil;
import com.shike.core.utils.redis.impl.RedisTokenKey;
import com.shike.dao.entity.SkUser;
import com.shike.web.common.BusCode;
import com.shike.web.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IdentityService {

    @Autowired
    private SkUserService skUserService;

    @Autowired
    private RedisUtil redisUtil;

    public R login(UserVo userVo) {
        SkUser userDB = skUserService.getByPhone(userVo.getPhone());
        if (userDB == null || !userDB.getPassword().equals(userVo.getPassword())){
            return R.PASSWORD_ERROR;
        }
        if (BusCode.UserState.NO.equals(userDB.getState())){
            return R.USER_DISABLE_ERROR;
        }
        JwtUserDto jwtUserDto = JwtUserDto.builder()
                .userId(userDB.getId())
                .phone(userDB.getPhone())
                .build();
        String token = JWTUtil.sign(jwtUserDto);

        redisUtil.putValue(RedisTokenKey.TOKEN_KEY, userDB.getId(),token);
        return R.successObj(token);
}
}
